package main

import (
	"context"
	"fmt"
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	. "fyne.io/fyne/v2/canvas"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/widget"
	"github.com/gogf/gf/v2/os/gcron"
	"github.com/gogf/gf/v2/os/gctx"
	"github.com/gogf/template-single/internal/cmd"
	"github.com/gogf/template-single/internal/ker/mod"
	_ "github.com/gogf/template-single/internal/packed"
	themex "github.com/zhaocy/fyne-ex"
	"image/color"
	"math/rand"
	"net"
	"net/url"
)

func main() {
	ctx := gctx.GetInitCtx()
	cmd.Main.Run(ctx)

	myApp := app.New()
	myApp.Settings().SetTheme(themex.Themex())
	myWin := myApp.NewWindow("ModBus")
	h := widget.NewHyperlink("ModBus接口", parseUrl(fmt.Sprintf("http://%s:%d/api/mdb/di", getIp(), cmd.S.GetListenedPort())))
	zft(myWin, h, ctx)
	myWin.Resize(fyne.NewSize(320, 200))
	myWin.ShowAndRun()

}

// 直方图
func zft(w fyne.Window, h *widget.Hyperlink, ctx context.Context) {
	c := container.NewWithoutLayout()

	data := genData(13, 50)
	var lines []*Line
	var txt *Text
	for i, d := range data {
		if i == 6 {
			line := NewLine(randColor())
			lines = append(lines, line)
			txt = NewText("", randColor())
			txt.Move(fyne.NewPos(float32(i*20), 0))
			c.Objects = append(c.Objects, txt)
			continue
		}
		line := NewLine(randColor())
		line.Position1 = fyne.NewPos(float32(i*20), 0)
		line.Position2 = fyne.NewPos(float32(i*20), d)
		line.StrokeWidth = 10
		lines = append(lines, line)
		c.Objects = append(c.Objects, line)
	}

	var x, n = 1, 3

	var ft = func() {
		var y = n - x
		var s = fmt.Sprintf("%d", y)
		if y == 0 {
			s = ""
		}
		txt.Text = s
		Refresh(txt)
	}

	var f = func(ctx context.Context) {
		ft()
		if x < n {
			x++
			return
		}
		if x >= n {
			x = 1
		}
		data = genData(13, 50)
		for i, y := range lines {
			if i == 6 {
				continue
			}
			y.Position2 = fyne.NewPos(float32(i*20), data[i])
			Refresh(y)
		}
	}

	var e *gcron.Entry
	zftBtn := widget.NewButton("刷新", nil)
	zftBtn.OnTapped = func() {
		if e == nil {
			zftBtn.SetText("停止")
			e, _ = gcron.Add(ctx, "0/1 * * * * *", f, "hello")
		} else {
			e.Stop()
			e.Close()
			e = nil
			x = 3
			ft()
			zftBtn.SetText("刷新")
		}
	}
	zftBtn.Resize(fyne.NewSize(70, 20))
	zftBtn.Move(fyne.NewPos(0, 100))
	w.SetContent(container.NewVBox(
		container.NewHBox(h, zftBtn),
		c))
}

func randColor() color.Color {
	return color.RGBA{uint8(rand.Intn(256)), uint8(rand.Intn(256)), uint8(rand.Intn(256)), 255}
}

func genData(count, max int) (data []float32) {
	di, _ := mod.Am.ReadDI()
	do, _ := mod.Am.ReadDO()
	var f = func(b bool, f1, f2 float32) float32 {
		if b {
			return f1
		}
		return f2
	}

	for i := 0; i < len(di); i++ {
		data = append(data, f(di[i], 50, 5))
	}
	data = append(data, 0)
	for i := 0; i < len(do); i++ {
		data = append(data, f(do[i], 60, 10))
	}
	return
}

func parseUrl(str string) *url.URL {
	u, err := url.Parse(str)
	if err != nil {
		fyne.LogError("url parse err:", err)
	}
	return u
}

func getIp() string {
	addrs, err := net.InterfaceAddrs()
	if err != nil {
		fmt.Println(err)
		return "127.0.0.1"
	}
	for _, address := range addrs {
		// 检查ip地址判断是否回环地址
		if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
			if ipnet.IP.To4() != nil {
				fmt.Println(ipnet.IP.String())
				return ipnet.IP.String()
			}
		}
	}
	return "127.0.0.1"
}
